package com.hbpa.daoImpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.hbpa.dao.StudentJlDao;
import com.hbpa.form.StudentJlItem;
import com.hbpa.util.ConnectionFactory;
import com.hbpa.util.JdbcUtil;
 
public class StudentJlDaoImpl implements StudentJlDao {

	public boolean addStudentJl(String xh, String sjd, String dw, String gz,
			String gzxz) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("insert into st_jl(xh,sjd,dw,gz,gzxz) values(?,?,?,?,?)");
			ps.setString(1, xh);
			ps.setString(2, sjd); 
			ps.setString(3, dw);
			ps.setString(4, gz); 
			ps.setString(5, gzxz); 
			ps.executeUpdate();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	public boolean checkStudentJl(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select count(*) as c from st_jl where xh = ?");
			
			ps.setString(1, xh);
			rs = ps.executeQuery();
			if(rs.next())
			{ 
				String count = rs.getString("c");
				if(!count.equals("0")){
					return true;
				}else{
					return false;
				}
			}
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		}
		return false;
	}

	
	public boolean deleteAllStudentJl(String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jl where xh=?");
			ps.setString(1,xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}
	
	
	public boolean deleteStudentJl(String id, String xh) {
		Connection con = null;
		PreparedStatement ps = null;
		try {
			con = ConnectionFactory.getConnection();
			con.setAutoCommit(false);
			ps = con.prepareStatement("delete from st_jl where id=? and xh=?");
			ps.setString(1, id);
			ps.setString(2, xh);
			
			ps.execute();
			con.commit();
			return true;
		} catch (SQLException e) {
			if(con!=null){try{con.rollback();}catch(Exception ex){}}
			e.printStackTrace();
		}finally{
			JdbcUtil.close(ps, con);
		}
		return false;
	}

	
	public List<StudentJlItem> getStudentJlByXh(String xh) {
		if(checkStudentJl(xh)==false)
		{
			return null;
		}
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null; 
		List<StudentJlItem> studentJlItem = new ArrayList<StudentJlItem>();
		try {
			con = ConnectionFactory.getConnection(); 
			ps = con.prepareStatement("select * from st_jl where xh = ? order by id asc");

			ps.setString(1, xh);
			rs = ps.executeQuery();
			while(rs.next())
			{   
				StudentJlItem item = new StudentJlItem();
				item.setId(rs.getString("id"));
				item.setXh(rs.getString("xh"));
				item.setDw(rs.getString("dw"));
				item.setGz(rs.getString("gz"));
				item.setSjd(rs.getString("sjd"));
				item.setGzxz(rs.getString("gzxz"));
				studentJlItem.add(item);
			}
			 return studentJlItem;
		} catch (SQLException e) { 
			e.printStackTrace();
		}finally{
			JdbcUtil.close(rs,ps,con);
		} 
		return null;
	}
  
}
